using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MauiBuilder.Services;
using System.Windows.Input;

namespace MauiBuilder.ViewModels
{
    /// <summary>
    /// 登录页面的视图模型，处理登录相关的业务逻辑
    /// </summary>
    public partial class LoginViewModel : BaseViewModel
    {
        /// <summary>
        /// 用户名
        /// </summary>
        [ObservableProperty]
        private string username = string.Empty;

        /// <summary>
        /// 密码
        /// </summary>
        [ObservableProperty]
        private string password = string.Empty;

        /// <summary>
        /// 是否正在加载中
        /// </summary>
        [ObservableProperty]
        private bool isLoading = false;

        /// <summary>
        /// 是否记住密码
        /// </summary>
        [ObservableProperty]
        private bool rememberPassword;

        /// <summary>
        /// 登录命令，处理用户登录逻辑
        /// </summary>
        [RelayCommand]
        private async Task OnLogin()
        {
            LogManager.Instance.LogInfo($"尝试登录，用户名: {Username}");
            
            if (string.IsNullOrWhiteSpace(Username) || string.IsNullOrWhiteSpace(Password))
            {
                LogManager.Instance.LogWarning("登录失败：用户名或密码为空");
                await AppShell.DisplayToastAsync("请输入账号和密码");
                return;
            }

            IsLoading = true;
            LogManager.Instance.LogInfo("开始登录流程");
            
            try
            {
                // 模拟登录过程
                await Task.Delay(1000);
                
                // 实际项目中应该调用API进行登录验证
                LogManager.Instance.LogInfo($"用户 {Username} 登录成功");
                await AppShell.DisplayToastAsync("登录成功");
                // 登录成功后的导航逻辑
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError("登录过程中发生异常", ex);
                await AppShell.DisplayToastAsync("登录失败，请稍后重试");
            }
            finally
            {
                IsLoading = false;
            }
        }

        /// <summary>
        /// 跳转到注册页面的命令
        /// </summary>
        [RelayCommand]
        private async Task OnGoToRegister()
        {
            LogManager.Instance.LogInfo("用户请求跳转到注册页面");
            await Shell.Current.GoToAsync("RegisterPage");
        }

        /// <summary>
        /// 忘记密码命令
        /// </summary>
        [RelayCommand]
        private async Task OnForgotPassword()
        {
            LogManager.Instance.LogInfo("用户点击了忘记密码");
            await AppShell.DisplayToastAsync("忘记密码功能将在后续版本中提供");
        }

        [RelayCommand]
        private async Task OnLoginWithWeChat()
        {
            await AppShell.DisplayToastAsync("微信登录功能将在后续版本中提供");
        }

        [RelayCommand]
        private async Task OnLoginWithWeibo()
        {
            await AppShell.DisplayToastAsync("微博登录功能将在后续版本中提供");
        }

        [RelayCommand]
        private async Task OnLoginWithQQ()
        {
            await AppShell.DisplayToastAsync("QQ登录功能将在后续版本中提供");
        }
    }
}